package com.afd.ozblip.activity;

import java.util.Locale;

import com.afd.ozblip.util.SharedUtil;

import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class LanguageActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private RadioGroup languageGg;
	private RadioButton chineseRb;
	private RadioButton englishRb;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.language);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.languageGg = (RadioGroup) findViewById(R.id.rg_language_group);
		this.chineseRb = (RadioButton) findViewById(R.id.rb_language_chinese);
		this.englishRb = (RadioButton) findViewById(R.id.rb_language_english);
		String language = SharedUtil.fetchLanguageInfo(this);
		if ("zh".equals(language)) {
			this.chineseRb.setChecked(true);
		} else {
			this.englishRb.setChecked(true);
		}
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		this.languageGg
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (checkedId == R.id.rb_language_chinese) {
							changecLanguage("zh");
						} else {
							changecLanguage("en");
						}
					}
				});
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	private void changecLanguage(String languageToLoad) {
		Locale locale = new Locale(languageToLoad);
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config, null);
		// 存储设置的语言
		SharedUtil.storeLanguageInfo(this, languageToLoad);
	}
}